package org.invigorare.auctions;

import java.util.*;

import org.invigorare.Utils;
import org.invigorare.auctions.data.ProduceDataOpener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class AuctionSaleContents extends Activity {

	public static final String KEY_PRODUCE_ID = "PRODUCE_ID";
	public static final String KEY_PRODUCE_NAME = "PRODUCE_NAME";
	public static final String KEY_WEIGHT = "WEIGHT";
	public static final String KEY_RATE = "RATE";
	
	public static final int SUCCESS_RETURN_CODE = 1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auction_sale_contents);
        
        associateFieldVars();
        
        /* Set up submit button that returns us to the last activity with result */
        submitButton.setOnClickListener(new ClickListener_AcceptContents());
        
        /* Set up Spinner (dropdown selection box) */
        setupProduceNames();
    }
	
    Button submitButton;
    Spinner selProduceName;
    EditText textWeight, textRate;
    
    private void associateFieldVars() {
    	submitButton = (Button) findViewById(R.id.button_contents_accept);
    	selProduceName = (Spinner) findViewById(R.id.contents_produceName);
    	textWeight = (EditText) findViewById(R.id.contents_weight);
    	textRate = (EditText) findViewById(R.id.contents_rate);
    }
    
    Map<String, Integer> produceNames_to_id; 
    private void setupProduceNames() {
    	Context context = getApplicationContext();
    	String languageCode = context.getString(R.string.languageCode);
    	produceNames_to_id = ProduceDataOpener.getProduceNames(context, languageCode);
    	
    	Set<String> sortedKeySet = new TreeSet<String>(produceNames_to_id.keySet());
    	List<String> sortedNames = new ArrayList<String>(sortedKeySet);
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, sortedNames);
    	selProduceName.setAdapter(adapter);
    	
    	for (String name : sortedNames) {
    		Log.i("AuctionSaleContents", name);
    	}
    }
    
    private class ClickListener_AcceptContents implements View.OnClickListener {

		public void onClick(View v) {
			
			/* Verify that all the fields have been populated */
			String produceName = (String) selProduceName.getSelectedItem();
			if (produceName == null) { //== Spinner.INVALID_POSITION) {
				showToast(R.string.contents_error_badProduce);
				return;
			}
			int produceId = produceNames_to_id.get(produceName); // convert name back to DB index
				
			float weight, rate;
			String weightStr = textWeight.getText().toString();
			String rateStr = textRate.getText().toString();
			
			/* Error checking on Weight and Rate */
			try {
				weight = Float.valueOf(weightStr);
				if (weight <= 0) {
					showToast(R.string.contents_error_badWeight);
					return;
				}
			} catch (NumberFormatException e) {
				showToast(R.string.contents_error_badWeight);
				return;
			}
			try {
				rate = Float.valueOf(rateStr);
				if (rate <= 0) {
					showToast(R.string.contents_error_badRate);
					return;
				}
			} catch (NumberFormatException e) {
				showToast(R.string.contents_error_badRate);
				return;
			}
			
			/* Store the requested data */
			Intent intent = new Intent();
			Bundle b = new Bundle();
			b.putInt(KEY_PRODUCE_ID, produceId);
			b.putString(KEY_PRODUCE_NAME, produceName);
			b.putFloat(KEY_WEIGHT, weight);
			b.putFloat(KEY_RATE, rate);
			intent.putExtras(b);
			setResult(SUCCESS_RETURN_CODE, intent);

			/* Pop this Activity */
			finish();
		}
		
		private void showToast(int stringResource) {
			Utils.showToast(AuctionSaleContents.this, getString(stringResource), Toast.LENGTH_LONG);
		}
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    	//ignore orientation change
    	super.onConfigurationChanged(newConfig);
    }
    
}
